package com.chenwut.threadDemo.waitandnotify;

/**
 * @author ChenPi
 * @since 2024/09/19 10:50
 */
public class Foodie extends Thread {
    @Override
    public void run() {
        while (true) {
            synchronized (Desk.lock) {
                if (Desk.count == 0) {
                    break;
                } else {
                    if (Desk.foodFlag == 0) {
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        Desk.count--;
                        System.out.println("吃货正在吃面条，面条剩余：" + Desk.count);
                        Desk.foodFlag = 0;
                        Desk.lock.notify();
                    }
                }

            }
        }
    }
}
